<template>
  <div class="edit-address-page">
    <!-- 头部 -->
    <div class="header">
      <div class="back" @click="$router.back()">
        <i class="iconfont icon-back"></i>
      </div>
      <div class="title">编辑地址</div>
    </div>

    <!-- 表单 -->
    <div class="form" v-if="form">
      <div class="form-item">
        <label>联系人：</label>
        <input type="text" v-model="form.contactName" placeholder="请输入联系人姓名">
      </div>
      <div class="form-item">
        <label>性别：</label>
        <div class="radio-group">
          <div class="radio" :class="{ active: form.contactSex === 1 }" @click="form.contactSex = 1">
            <span>先生</span>
          </div>
          <div class="radio" :class="{ active: form.contactSex === 0 }" @click="form.contactSex = 0">
            <span>女士</span>
          </div>
        </div>
      </div>
      <div class="form-item">
        <label>手机号：</label>
        <input type="tel" v-model="form.contactTel" placeholder="请输入手机号">
      </div>
      <div class="form-item">
        <label>收货地址：</label>
        <input type="text" v-model="form.address" placeholder="请输入详细地址">
      </div>
    </div>

    <!-- 保存按钮 -->
    <div class="save-btn" @click="updateAddress">保存</div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const route = useRoute()
const form = ref(null)

// 获取地址信息
const getAddressInfo = async () => {
  try {
    const res = await axios.get('/deliveryAddress/getDeliveryAddressById', {
      params: { daId: route.query.daId }
    })
    form.value = res.data
  } catch (error) {
    console.error('获取地址信息失败:', error)
  }
}

// 更新地址
const updateAddress = async () => {
  // 表单验证
  if (!form.value.contactName) {
    alert('请输入联系人姓名')
    return
  }
  if (!form.value.contactTel) {
    alert('请输入手机号')
    return
  }
  if (!/^1[3-9]\d{9}$/.test(form.value.contactTel)) {
    alert('请输入正确的手机号')
    return
  }
  if (!form.value.address) {
    alert('请输入详细地址')
    return
  }

  try {
    await axios.post('/deliveryAddress/updateDeliveryAddress', null, {
      params: form.value
    })
    router.back()
  } catch (error) {
    console.error('更新地址失败:', error)
  }
}

onMounted(() => {
  getAddressInfo()
})
</script>


<style scoped>
.edit-address-page {
  padding-top: 44px;
  padding-bottom: 60px;
  background: #f5f5f5;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background: #007bff; /* Changed color */
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 15px;
  z-index: 100;
}

.back {
  padding: 10px;
  margin-left: -10px;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 18px; /* Increased font size */
}

.form {
  background: #fff;
  margin-top: 10px;
}

.form-item {
  display: flex;
  align-items: center;
  padding: 20px; /* Increased padding */
  border-bottom: 1px solid #eee;
}

.form-item label {
  width: 80px;
  color: #333;
}

.form-item input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 14px;
}

.radio-group {
  flex: 1;
  display: flex;
}

.radio {
  padding: 5px 15px;
  border: 1px solid #ddd;
  border-radius: 20px;
  margin-right: 10px;
  font-size: 14px;
  color: #666;
}

.radio.active {
  background: #007bff; /* Changed color */
  color: #fff;
  border-color: #007bff;
}

.save-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: #007bff; /* Changed color */
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold; /* Added bold text */
}
</style>